package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivityJicaiSearchTwoBinding;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.view.fragment.JiCaiChangjiaSecondFragment;
import com.sgb.jicai.view.fragment.JiCaiXuFangSecondFragment;
import com.sgb.jicai.viewmodel.JiCaiSearchTwoModel;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.DisplayUtil;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;

/**
 * 集采搜索二级页面
 */
public class JiCaiSearchTwoActivity extends AppActivity {

    JiCaiSearchTwoModel model;
    ActivityJicaiSearchTwoBinding binding;
    private static final String KEY_SEARCH_WORDS = "searchJicaiListWords";
    private String searchWords = "";
    private SlidingFragmentPagerAdapter mPagerAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private JiCaiXuFangSecondFragment jiCaiXuFangSecondFragment;
    private JiCaiXuFangSecondFragment jiCaiQiYeSecondFragment;
    private JiCaiChangjiaSecondFragment jiCaiChangjiaSecondFragment;
    private String[] mTitles = new String[3];

    public static void start(Context context, String searchWords) {
        Intent intent = new Intent();
        intent.putExtra(KEY_SEARCH_WORDS, searchWords);
        intent.setClass(context, JiCaiSearchTwoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_jicai_search_two);
        model = new ViewModelProvider(this).get(JiCaiSearchTwoModel.class);
        model.setActivity(JiCaiSearchTwoActivity.this);
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.tvSearch.setOnClickListener(this::onMultiClick);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void init() {
        super.init();
        searchWords = getIntent().getStringExtra(KEY_SEARCH_WORDS);
        binding.editSearch.setText(searchWords);
        jiCaiXuFangSecondFragment = JiCaiXuFangSecondFragment.newInstance("1","1",searchWords);
        jiCaiChangjiaSecondFragment = JiCaiChangjiaSecondFragment.newInstance(false,searchWords);
        jiCaiQiYeSecondFragment = JiCaiXuFangSecondFragment.newInstance("2","1",searchWords);
        initViewPager();
    }

    private void initViewPager() {
        mTitles[0] = "需方发起";
        mTitles[1] = "供方发起";
        mTitles[2] = "企业专项";
        mFragments.add(jiCaiXuFangSecondFragment);
        mFragments.add(jiCaiChangjiaSecondFragment);
        mFragments.add(jiCaiQiYeSecondFragment);
        mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewPager.setAdapter(mPagerAdapter);
        binding.viewPager.setOffscreenPageLimit(mTitles.length);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
        binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        updateTitleNum();
    }



    public void updateTitle(int index, int count) {
        String num = " (" + (count > 99 ? "99+" : String.valueOf(count)) + ")";
        String titleText = null;
        if (index == 0) {
            titleText = "需方发起" + num;
        } else if (index == 1) {
            titleText = "供方发起" + num;
        } else if (index == 2) {
            titleText = "企业专项" + num;
        }
        if (titleText == null) {
            return;
        }
        int start = titleText.indexOf("(");
        int end = titleText.length();
        SpannableString textSpan = new SpannableString(titleText);
        textSpan.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(this, 14)), 0, start, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(this, 10)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        try {
            if (index == 0) {
                binding.tabLayout.getTabAt(0).setText(textSpan);
            } else if (index == 1) {
                binding.tabLayout.getTabAt(1).setText(textSpan);
            } else if (index == 2) {
                binding.tabLayout.getTabAt(2).setText(textSpan);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_search) {
            searchWords = binding.editSearch.getText().toString().trim();
            updateTitleNum();
            EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FOUR_TYPE_SEARCH, binding.editSearch.getText().toString().trim()));
        }
    }

    private void updateTitleNum() {
        model.getXuFangCount(searchWords);
        model.getFactoryCount(searchWords);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JiCaiEventEntity event) {
        if (event.getCode() == EventConstant.EVENT_JICAI_TOP_REFRESH_VISIBLE) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public String getKeyWords() {
        String key = binding.editSearch.getText().toString().trim();
        return TextUtils.isEmpty(key) ? "" : key;
    }
}
